package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.MessagesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 环信消息接口
 */
@Slf4j
@RestController
@RequestMapping("/messages")
public class MessagesController {
    @Autowired
    private MessagesService messagesService;

    /**
     * 查询用户信息
     *
     * @param huanxinId 环信id
     * @return 返回用户详细信息
     */
    @GetMapping("/userinfo")
    public ResponseEntity<UserInfoVo> userinfo(String huanxinId) {
//        String huanxinId = (String) map.get("huanxinId");
        UserInfoVo vo = messagesService.findUserInfoByHuanxin(huanxinId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 添加好友
     *
     * @param map 封装传过来的好友id
     * @return 无返回
     */
    @PostMapping("/contacts")
    public ResponseEntity<String> contacts(@RequestBody Map map) {
        Long userId = Long.valueOf(map.get("userId").toString());
        messagesService.contacts(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询联系人列表
     *
     * @param page     页码
     * @param pagesize 当页条数
     * @param keyword  条件
     * @return 联系人信息
     */
    @GetMapping("/contacts")
    public ResponseEntity<PageResult> contacts(@RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "10") Integer pagesize,
                                               String keyword) {
        PageResult pageResult = messagesService.findFriends(page, pagesize, keyword);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询公告
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/announcements")
    public ResponseEntity<PageResult> announcements(@RequestParam(defaultValue = "1") Integer page,
                                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = messagesService.announcements(page, pagesize);
        log.info(pageResult.toString());
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询喜欢列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/loves")
    public ResponseEntity<PageResult> loves(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = messagesService.loves(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询点赞列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/likes")
    public ResponseEntity<PageResult> likes(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = messagesService.likes(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询评论列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/comments")
    public ResponseEntity<PageResult> comments(@RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = messagesService.comments(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }
}
